//
// Created by Kqj on 25-9-1.
//

#include "cmd.h"
#include <stdio.h>
#include <stdlib.h>
#include <main.h>  // 用于_getch()函数获取按键

extern TIM_HandleTypeDef htim1;


extern uint8_t key_value;
extern uint8_t keystatue;
// 函数声明
void entry_main_screen(SystemState *state);
void enter_setting_mode(SystemState *state);
void update_pressure(SystemState *state);
void handle_pump_operation(SystemState *state);


extern SystemState state;
extern int8_t key;
extern int8_t key_statue;
extern SettingOption current_setting ;
extern SystemState temp_state ; // 临时存储设置，退出时生效
extern uint8_t entry_setting_flag;

extern uint16_t adc1_V;
extern uint16_t adc2_V;
extern int16_t temp;


int cmd() {


    // 按键说明: 1-启停气泵  2-进入设置  3-减小参数  4-增大参数  ESC-退出程序\n

        update_pressure(&state);
        handle_pump_operation(&state);


    if(entry_setting_flag == 0)
    {

        entry_main_screen(&state);
        if(entry_setting_flag == 1)
        {
            temp_state = state; // 临时存储设置，退出时生效
            current_setting = SETTING_MODE;
        }
    }
    if(entry_setting_flag == 1)
    {
        enter_setting_mode(&state);
    }

}


// 显示主界面


void entry_main_screen(SystemState *state) {
    show_info(state->mode,state->pump_running);
    Fresh_Tenser_data();
    // 检查按键输入
    if (key) {
        if (key == 1) { // 按键1: 启停气泵
            HAL_Delay(10);
            key = 0;
            state->pump_running = !state->pump_running;
            if (state->pump_running) {
                printf("气泵已启动\n");
            } else {
                printf("气泵已停止\n");
            }
        } else if (key == 2) { // 按键2: 进入设置模式
            HAL_Delay(10);
            key = 0;
            entry_setting_flag = 1;
        }
    }
}

// 进入设置模式
void enter_setting_mode(SystemState *state) {
    show_info2();
    Fresh_Tenser_data();
        // 显示当前设置选项

        // 等待按键输入

        show_right_info(temp_state.mode,temp_state.power,temp_state.min_threshold,temp_state.max_threshold);
        show_square(current_setting);
        if (key == 1) { // 按键1: 保存设置并退出
            HAL_Delay(10);
            key=0;
            *state = temp_state; // 保存临时设置
            printf("设置已保存\n");
            entry_setting_flag = 0;
        } else if (key == 2) { // 按键2: 切换设置选项
            HAL_Delay(10);
            key=0;
            current_setting = (current_setting + 1) % SETTING_COUNT;
        } else if (key == 3) { // 按键3: 减小参数
            HAL_Delay(10);
            key=0;
            switch (current_setting) {
                case SETTING_MODE:
                    temp_state.mode = (temp_state.mode == MODE_MANUAL) ? MODE_CONSTANT : MODE_MANUAL;
                    break;
                case SETTING_POWER:
                    temp_state.power = (temp_state.power > 0) ? temp_state.power - 5 : 0;
                    break;
                case SETTING_MIN_THRESH:
                    temp_state.min_threshold = (temp_state.min_threshold > 20) ? temp_state.min_threshold - 5 : 20;
                    // 确保最小阈值小于最大阈值
                    if (temp_state.min_threshold >= temp_state.max_threshold) {
                        temp_state.max_threshold = temp_state.min_threshold + 5;
                    }
                    break;
                case SETTING_MAX_THRESH:
                    temp_state.max_threshold = (temp_state.max_threshold > 25) ? temp_state.max_threshold - 5 : 25;
                    // 确保最大阈值大于最小阈值
                    if (temp_state.max_threshold <= temp_state.min_threshold) {
                        temp_state.min_threshold = temp_state.max_threshold - 5;
                    }
                    break;
            }
        } else if (key == 4) { // 按键4: 增大参数
            HAL_Delay(10);
            key=0;
            switch (current_setting) {
                case SETTING_MODE:
                    temp_state.mode = (temp_state.mode == MODE_MANUAL) ? MODE_CONSTANT : MODE_MANUAL;
                    break;
                case SETTING_POWER:
                    temp_state.power = (temp_state.power < 100) ? temp_state.power + 5 : 100;
                    break;
                case SETTING_MIN_THRESH:
                    temp_state.min_threshold = (temp_state.min_threshold < 50) ? temp_state.min_threshold + 5 : 50;
                    // 确保最小阈值小于最大阈值
                    if (temp_state.min_threshold >= temp_state.max_threshold) {
                        temp_state.max_threshold = temp_state.min_threshold + 5;
                    }
                    break;
                case SETTING_MAX_THRESH:
                    temp_state.max_threshold = (temp_state.max_threshold < 65) ? temp_state.max_threshold + 5 : 65;
                    break;
            }
        }

}

// 模拟气压更新
void update_pressure(SystemState *state) {
    Fresh_Tenser_data();
    state->current_pressure = adc1_V;
}

// 处理气泵自动运行逻辑
void handle_pump_operation(SystemState *state) {
    if(state->pump_running == 0)
    {
        __HAL_TIM_SetCompare(&htim1,TIM_CHANNEL_2,0 );
    }
    else if (state->mode == MODE_MANUAL) {
        // 手动模式下的自动控制
            // 如果气压达到最大阈值，自动停止
            if (state->current_pressure >= state->max_threshold ) {
                __HAL_TIM_SetCompare(&htim1,TIM_CHANNEL_2,0 );
                printf("已达到最大阈值，气泵自动停止\n");
            }
            // 如果气压低于最小阈值，自动启动
            if (state->current_pressure < state->min_threshold) {
                __HAL_TIM_SetCompare(&htim1,TIM_CHANNEL_2,100 );
                printf("已低于最小阈值，气泵自动启动\n");
            }
    }
    else if(state->mode ==  MODE_CONSTANT)
    {
        // 恒压模式下的自动控制
            // 如果气压达到最大阈值，自动停止
            if (state->current_pressure >= state->max_threshold) {
                __HAL_TIM_SetCompare(&htim1,TIM_CHANNEL_2,0 );
                printf("已达到最大阈值，气泵自动停止\n");
            }
            // 如果气压低于最小阈值，自动启动
            if (state->current_pressure < state->min_threshold) {
                __HAL_TIM_SetCompare(&htim1,TIM_CHANNEL_2,state->power );
                printf("已低于最小阈值，气泵自动启动\n");
            }
    }



        // 这里可以根据需要实现更复杂的恒压控制逻辑
}

